﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using System.Net;
using System.Net.Sockets;

namespace OINS_VS08
{
    class Server2
    {
        class ClientHandler
        {
            private byte[] buffer;
            private Socket socket;
            private Stream inputstream;
           
            private NetworkStream networkStream;
            private AsyncCallback callbackRead;
            private AsyncCallback callbackWrite;
            private AsyncCallback myFileCallBack;
            private AsyncCallback mySearchDemand;
           
           

            public ClientHandler(Socket socketForClient)
            {
                socket = socketForClient;
                buffer = new byte[256];
                networkStream = new NetworkStream(socketForClient);//new NetworkStream(socketForClient);
                callbackRead = new AsyncCallback(this.OnReadComplete);
                callbackWrite = new AsyncCallback(this.OnWriteComplete);
                myFileCallBack = new AsyncCallback(this.OnFileCompletedRead);
                mySearchDemand = new AsyncCallback(this.SearchDemand);
                
            }
            public void StartRead()
            {
                networkStream.BeginRead(buffer, 0, buffer.Length, callbackRead, null);
            }
            private void SearchDemand(IAsyncResult a)
            {
                int bytesRead = inputstream.EndRead(a);
                if (bytesRead > 0)
                {
                    Console.WriteLine("podaj nazwe pliku");
                    StartRead();

                }
                else
                    Console.WriteLine("zerwano polaczenie");
            }



            private void OnReadComplete(IAsyncResult ar)
            {
                int bytesRead = networkStream.EndRead(ar);

                if (bytesRead > 0)
                {

                    string demand = System.Text.Encoding.ASCII.GetString(buffer, 0, bytesRead);
                    Console.Write("Otrzymane żądanie {0}", demand);
                    //inputstream = File.OpenRead(fileNa32me);
                    inputstream.BeginRead(buffer, 0, buffer.Length, myFileCallBack, null);
                }
                else
                {
                    Console.WriteLine("Polaczenie zostalo zerwane");
                    networkStream.Close();
                    socket.Close();
                    networkStream = null;
                    socket = null;
                }
            }
            void OnFileCompletedRead(IAsyncResult asyncResult)
            {
                int bytesRead = inputstream.EndRead(asyncResult);
                if (bytesRead > 0)
                {
                    networkStream.BeginWrite(buffer, 0, bytesRead, callbackWrite, null);
                }
                else
                {
                    Console.WriteLine(" Koniec. ");
                    networkStream.Close();
                    socket.Close();
                    networkStream = null;
                    socket = null;
                }
            }

            private void OnWriteComplete(IAsyncResult ar)
            {
                networkStream.EndWrite(ar);
                Console.WriteLine(" Zapis zostal ukonczony ");
                inputstream.BeginRead(buffer, 0, buffer.Length, myFileCallBack, null);
            }
        
            public void WaitingForRequest()
            {
                System.IO.StreamWriter streamwriter = new System.IO.StreamWriter(networkStream);
                System.IO.StreamReader streamreader = new System.IO.StreamReader(networkStream);

                String request;
               
                request=streamreader.ReadLine();
                if (request == "Hello aP2P Server")
                {
                    streamwriter.WriteLine("Hello aP2P Client");
                    streamwriter.Flush();
                    System.Console.WriteLine("a tu nie jerweeserstem tu");
                    request = streamreader.ReadLine();
                    System.Console.WriteLine("a tchuj  sgsgsgsgsdgem tu");
                    if (request == "search")
                    {
                        streamwriter.WriteLine("Podaj nazwe pliku lub jego typ");
                        System.Console.WriteLine("ihaaaaa");
                        streamwriter.Flush();
                        request=streamreader.ReadLine();
                        System.Console.WriteLine("to jest odpowiedz: {0}", request);

                    }
                    if (request == "")
                    {
                        streamwriter.WriteLine("sgfsdg");
                        streamwriter.Flush();
                    
                    }
                    System.Console.WriteLine("a tupizda tu");
                }
            }
        }

        


        public void Run()
        {
            IPAddress localAddr = IPAddress.Parse("127.0.0.1");
            TcpListener tcpListener = new TcpListener(localAddr, 65000);
            tcpListener.Start();
            
            
            
            for (; ; )
            {
                Socket socketForClient = tcpListener.AcceptSocket();
               // socketForClient.NoDelay = true;
                if (socketForClient.Connected)
                {
                    Console.WriteLine("Klient zostal podlaczony");
                    
               
                    ClientHandler handler = new ClientHandler(socketForClient);
                    
                    handler.WaitingForRequest();
                }
            }
        }
    }
}

